package com.retoc.oawx.controller;

import cn.hutool.json.JSONUtil;
import com.retoc.oawx.common.util.R;
import com.retoc.oawx.domain.Role;
import com.retoc.oawx.domain.req.DeleteRoleReq;
import com.retoc.oawx.domain.req.InsertRoleReq;
import com.retoc.oawx.domain.req.RoleReq;
import com.retoc.oawx.domain.req.UpdateRoleReq;
import com.retoc.oawx.exception.OAException;
import com.retoc.oawx.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
@Api("角色模块网络接口")
public class RoleController {

    @Autowired
    public RoleService roleService;

    @PostMapping("/searchRoleOwnPermission")
    @ApiOperation("查询角色权限列表")
    @RequiresPermissions(value = {"ROOT","ROLE:SELECT"},logical = Logical.OR)
    public R searchRoleOwnPermission(@Valid @RequestBody RoleReq req) {
        ArrayList list=roleService.searchRoleOwnPermission(req.getId());
        return R.ok().put("result",list);
    }

    @GetMapping("/searchAllRole")
    @ApiOperation("查询所有角色数据")
    public R searchAllRole() {
        List<Role> list = roleService.searchAllRole();
        return R.ok().put("result", list);
    }

    @PostMapping("/deleteRoleById")
    @ApiOperation("根据ID删除角色")
    @RequiresPermissions(value = {"ROOT","ROLE:DELETE"},logical = Logical.OR)
    public R deleteRoleById(@Valid @RequestBody DeleteRoleReq req){
        roleService.deleteRoleById(req.getId());
        return R.ok().put("result","success");
    }

    @GetMapping("/searchAllPermission")
    @ApiOperation("查询所有权限")
    @RequiresPermissions(value = {"ROOT","ROLE:SELECT"},logical = Logical.OR)
    public R searchAllPermission(){
        ArrayList list=roleService.searchAllPermission();
        return R.ok().put("result",list);
    }

    @PostMapping("/insertRole")
    @ApiOperation("添加角色")
    @RequiresPermissions(value = {"ROOT","ROLE:INSERT"},logical = Logical.OR)
    public R insertRole(@Valid @RequestBody InsertRoleReq req) {
        if(!JSONUtil.isJsonArray(req.getPermissions())){
            throw new OAException("权限不是数组格式");
        }
        Role entity=new Role();
        entity.setRoleName(req.getRoleName());
        entity.setPermissions(req.getPermissions());
        roleService.insertRole(entity);
        return R.ok().put("result","success");
    }

    @PostMapping("/updateRolePermissions")
    @ApiOperation("修改角色")
    @RequiresPermissions(value = {"ROOT","ROLE:UPDATE"},logical = Logical.OR)
    public R updateRolePermissions(@Valid @RequestBody UpdateRoleReq req) {
        if(!JSONUtil.isJsonArray(req.getPermissions())){
            throw new OAException("权限不是数组格式");
        }
        Role entity=new Role();
        entity.setId(req.getId());
        entity.setPermissions(req.getPermissions());
        roleService.updateRolePermissions(entity);
        return R.ok().put("result","success");
    }

}

